#include "StdTcp.h"
#include "StdThread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ClientFunc.h"


static char MyName[NameSize];

#define ip "47.98.33.10"
#define port 8080

void AnalysisRecv(MSG *m)
{
    switch (m->flag)
    {
    case CHAT:
    case BROADCAST:
         printf("接受来自|%s|的消息：%s\n",m->FromName,m->Content);
         break;
    case ONLINELIST:
        ShowOnlineList(m);
        break;
    case SENDFILE:
        WriteToLocal(m);
        break;
    case UPLOAD:
        if(strcmp(m->Content,"OK") == 0)
        {
            printf("上传文件成功!\n");
        }
        break;
    case FILELIST:
       ShowFileList(m); 
       break;
    case DOWNLOAD:
       WriteToLocal(m);
       break;
    default:
        break;
    }
}

void *ThreadHandle(void* arg)
{   
    TcpC *c = (TcpC*)arg; //将参数 arg 转换为 TcpC 类型的指针，并赋值给变量 c，这个指针通常指向一个 TCP 客户端结构体或对象
    while(1)
    {
        MSG m;
      if(TcpClientRecv(c,&m,sizeof(m)) == false) //调用 TcpClientRecv 函数从 TCP 客户端 c 中接收消息，如果接收失败，则执行下面的代码块。
      {
           printf("读线程接受错误！\n");
           exit(-1);
      }
      AnalysisRecv(&m);
    }
}

void LoginSignUp(TcpC*c)
{
    while(1)
    {
        int choice = LoginFace();
        MSG m;
        switch (choice)
        {
        case LOGIN:
            m = Login(MyName);
            TcpClientSend(c,&m,sizeof(MSG));
            if(TcpClientRecv(c,&m,sizeof(MSG)) == false)
            {
                exit(-1);
            }
            if(IsLoginSuccess(&m) == true)
            {
                return;
            }
            break;
        case SIGNUP:
             m = SignUp(MyName);
             TcpClientSend(c,&m,sizeof(MSG));
            if(TcpClientRecv(c,&m,sizeof(MSG)) == false)
            {
                exit(-1);
            }
            if(IsSignUpSuccess(&m) == true)
            {
                return;
            }
            break;
        default:
            break;
        }
        sleep(2);
         system("clear");
    }
}

void SendFileToServer(MSG*m,TcpC*c)
{            
    char* fileName = GetFileNameFromPath(m->Content);
    //   printf("fileName : %s\n",fileName); 
    PackFile *pf = PacketFileInit(m->Content);

    memset(m->Content,0,sizeof(m->Content));
    strcpy(m->Content + 4,fileName);
    int size = 1000;
    //   int count = 0;
    while(IsFileEnd(pf) == false)
    {
        ReadFromFile(pf,m->Content + 24,&size);
        //填入读取的文件内容大小
        memcpy(m->Content,&size,sizeof(int));

        TcpClientSend(c,m,sizeof(MSG));
        //     count++;
        //     printf("这是第%d包数据，大小%d\n",count,size);
        //   
    }
    size =-1 ;
    memcpy(m->Content,&size,sizeof(int));
    TcpClientSend(c,m,sizeof(MSG));
    free(fileName);
    ClearPacketFile(pf);
}



int main(int argc,char *argv[])
{   
    // if(argc != 3)
    // {
    //     printf("参数数量不对\n");
    //     return -1;
    // }
    // TcpC*c = TcpClientInit(argv[1],atoi(argv[2]));
    TcpC*c = TcpClientInit(ip,port);

    LoginSignUp(c);
    
    Thread*t = ThreadInit(ThreadHandle,c); //调用 ThreadInit 函数初始化一个线程，并将线程处理函数 ThreadHandle 和 TCP 客户端指针 c 作为参数
    while(1)
    {   

        int choice = Interface();
        MSG m;
        switch(choice)
        {   
             //私聊
            case CHAT:
                 m = ChatToClient(); //调用 ChatToClient 函数获取用户要发送的私聊消息，并将消息存储在变量 m 中。
                 strcpy(m.FromName,MyName); //将当前用户的用户名复制到消息结构体中的发送者名称字段
                TcpClientSend(c,&m,sizeof(m)); //调用 TcpClientSend 函数向服务器发送私聊消息
                break;
            //广播
           case BROADCAST:
                   m = ChatBroadCast();
                 strcpy(m.FromName,MyName);
                 TcpClientSend(c,&m,sizeof(m));
                break; 
            case ONLINELIST:
                  m = GetServerOnlineList();
                  strcpy(m.FromName,MyName);
                  TcpClientSend(c,&m,sizeof(m));
                  break;
            case SENDFILE:
                  m = SendFile();
                  strcpy(m.FromName,MyName);
                  SendFileToServer(&m,c);
                  break;
            case MYCLOUD:
                 while(1)
                 {
                    int choice = CloudInterface();
                    strcpy(m.FromName,MyName);
                     switch (choice)
                   {
                    case FILELIST:
                    {
                        m.flag = FILELIST;
                        TcpClientSend(c,&m,sizeof(m));
                        break;
                    }
                    case UPLOAD:
                        m = UpLoadFile();
                        strcpy(m.FromName,MyName);
                        SendFileToServer(&m,c);
                        
                        break;
                    case DOWNLOAD:
                        m = download();
                        strcpy(m.FromName,MyName);
                        SendFileToServer(&m,c);

                        break;
                    default:
                        break;
                    }

                 }
           default:
              break;

        }
       
    
       
       
    }   
}